package com.zcc.makingfriend.controller;

import com.zcc.makingfriend.dao.User;
import com.zcc.makingfriend.dto.PageDto;
import com.zcc.makingfriend.service.UserService;
import com.zcc.makingfriend.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaomi
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation("查找所有的用户")
    @GetMapping("/getAll")
    public Result getAll(){
        List<User> all = userService.getAll();
        return Result.success(all);
    }

    @ApiOperation("通过用户名查找用户")
    @GetMapping("/getUser/{username}")
    public Result getUserByName(@PathVariable String username) {
        User userByName = userService.getUserByName(username);
        return Result.success(userByName);
    }

    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result updateUser(@RequestBody User user) {
        int i = userService.updateUser(user);
        if (i == -1) {
            return Result.fail("电话号码输入错误");
        } else if (i == -2) {
            return Result.fail("邮箱输入错误");
        } else if (i == 1) {
            return Result.success("修改用户成功");
        }
        return Result.fail("修改用户失败");
    }

    @ApiOperation("删除用户")
    @PutMapping("/delete")
    public Result deleteUser(@PathVariable String uuid) {
        int i = userService.deleteUser(uuid);
        if (i == 1) {
            return Result.success("删除用户成功");
        }
        return Result.fail("删除用户失败");
    }

    @ApiOperation("通过id查找用户")
    @GetMapping("/getUserById/{id}")
    public Result getUserById(@PathVariable Long id) {
        User user = userService.findUserById(id);
        if (user != null) {
            return Result.success(user);
        }
        return Result.fail("查找用户失败");
    }

    @ApiOperation("统计所有用户数量")
    @GetMapping("/countAllUsers")
    public Result countAllUsers() {
        return Result.success(userService.countUsers());
    }

    @ApiOperation("模糊查询用户")
    @PostMapping("/findUsersByCondition")
    public Result findUsersByCondition(@RequestBody PageDto pageDto) {
        return Result.success(userService.findUsersByCondition(pageDto));
    }

    @ApiOperation("模糊查询用户数量")
    @PostMapping("/countUsersByCondition")
    public Result countUsersByCondition(@RequestBody PageDto pageDto) {
        return Result.success(userService.countUsersByCondition(pageDto));
    }

}
